import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:nft_shop/my/model/models.dart';
import 'package:nft_shop/net/dio_api.dart';
import 'package:nft_shop/net/service_repository.dart';
import 'package:nft_shop/res/global.dart';
import 'package:nft_shop/util/select_image_utils.dart';
import 'package:nft_shop/widgets/image.dart';
import 'package:nft_shop/widgets/load_image.dart';
import 'package:nft_shop/widgets/my_button.dart';
import 'package:nft_shop/widgets/my_text_field.dart';

class MyNewAlbumWidget extends StatefulWidget {
  AlbumListModel? album;
  VoidCallback onPressed;

  MyNewAlbumWidget({Key? key, this.album, required this.onPressed})
      : super(key: key);

  @override
  State<MyNewAlbumWidget> createState() => _MyNewAlbumWidgetState();
}

class _MyNewAlbumWidgetState extends State<MyNewAlbumWidget> {
  TextEditingController controller1 = TextEditingController();

  TextEditingController controller2 = TextEditingController();

  FocusNode _nodeText1 = FocusNode();

  FocusNode _nodeText2 = FocusNode();

  String urlStr = "";

  @override
  void initState() {
    super.initState();
    if (!ObjectUtil.isEmpty(widget.album)) {
      controller1.text = widget.album!.specialAlbumTitile ?? "";
      controller2.text = widget.album!.specialAlbumRate ?? "";
      urlStr = widget.album!.specialAlbumThumb ?? "";
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        height: 380 +
            (ScreenUtil.getBottomBarH(context) == 0
                ? 25
                : ScreenUtil.getBottomBarH(context)),
        padding: EdgeInsets.symmetric(horizontal: 15),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.only(
              topLeft: Radius.circular(10.0), topRight: Radius.circular(10.0)),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
                padding: EdgeInsets.only(top: 13),
                child: Center(
                    child: Text(
                  ObjectUtil.isEmpty(widget.album) ? "新增专辑" : "编辑专辑",
                  style: TextStyle(
                      fontSize: 16,
                      color: Global.kTheme.textPrimary4,
                      fontFamily: Global.mySemiboldFont),
                ))),
            SizedBox(
              height: 30,
            ),
            _itemWidget(
                title: "作品专题",
                inpitStr: "请输入作品专题名称",
                node: _nodeText1,
                controller: controller1,
                isNum: false),
            _itemWidget(
                title: "版权金",
                inpitStr: "请输入您的创作者版权金",
                rightStr: "%",
                node: _nodeText2,
                controller: controller2),
            SizedBox(
              height: 27,
            ),
            Text(
              "上传专题宣传图",
              style: TextStyle(fontSize: 14, color: Global.kTheme.textPrimary6),
            ),
            SizedBox(
              height: 5,
            ),
            InkWell(
                onTap: () {
                  _nodeText1.unfocus();
                  _nodeText2.unfocus();
                  _showHintDialog((url) {
                    setState(() {
                      urlStr = url;
                    });
                  });
                },
                child: ClipRRect(
                  borderRadius: BorderRadius.circular(4),
                  child: Container(
                    width: 77,
                    height: 77,
                    color: Global.kTheme.colorLine,
                    child: Center(
                        child: WrapperImage(
                      url: urlStr,
                      width: 77,
                      height: 77,
                      placeholder: Center(
                        child: LoadAssetImage(
                          "icon_add",
                          width: 25,
                          height: 25,
                        ),
                      ),
                    )),
                  ),
                )),
            SizedBox(
              height: 20,
            ),
            Container(
              height: 40,
              decoration: BoxDecoration(
                color: Global.kTheme.colorBackground3,
                borderRadius: BorderRadius.circular(4),
              ),
              child: MyButton(
                minHeight: 40,
                onPressed: () async {
                  _nodeText1.unfocus(); // 失去焦点
                  _nodeText2.unfocus(); // 失去焦点

                  if (controller1.text.length == 0) {
                    EasyLoading.showToast('请输入作品专题名称');
                    return;
                  }

                  if (controller2.text.length == 0) {
                    EasyLoading.showToast('请输入您的创作者版权金');
                    return;
                  }

                  if (urlStr.length == 0) {
                    EasyLoading.showToast('请上传专题宣传图');
                    return;
                  }
                  EasyLoading.show(status: "正在加载...");
                  try {
                    await ServiceRepository.specialAlbumSave(
                        id: ObjectUtil.isEmpty(widget.album)
                            ? ""
                            : widget.album!.specialAlbumId,
                        titile: controller1.text,
                        thumb: urlStr,
                        rate: controller2.text);
                    widget.onPressed();
                    Get.back();
                    EasyLoading.showToast("创建成功,审核1-3个工作日给结果");
                  } catch (e, s) {
                    EasyLoading.showToast(http.setError(e, s));
                  }
                },
                text: "确定",
                fontSize: 16,
                textColor: Colors.white,
                backgroundColor: Global.kTheme.colorBackground3,
              ),
            )
          ],
        ));
  }

  //cell
  Widget _itemWidget(
      {String? title,
      String? inpitStr,
      String? rightStr,
      bool isNum = false,
      FocusNode? node,
      TextEditingController? controller}) {
    return Container(
        height: 60,
        decoration: BoxDecoration(
            border: Border(
          bottom: Divider.createBorderSide(Get.context!,
              color: Global.kTheme.colorLine, width: 1),
        )),
        child: Row(
          children: [
            Container(
                width: 130,
                child: Text(
                  title!,
                  style: TextStyle(
                      fontSize: 14, color: Global.kTheme.textPrimary6),
                )),
            Expanded(
              child: TextField(
                controller: controller,
                focusNode: node,
                keyboardType: isNum
                    ? TextInputType.numberWithOptions(decimal: true)
                    : null,
                decoration: InputDecoration(
                  hintText: inpitStr,
                  border: InputBorder.none, // 去掉下滑线
                ),
              ),
            ),
            Visibility(
                visible: rightStr == null ? false : true,
                child: Text(
                  rightStr ?? "",
                  style: TextStyle(
                      fontSize: 15,
                      color: Global.kTheme.textPrimary6,
                      fontFamily: Global.mySemiboldFont),
                ))
          ],
        ));
  }

  //弹出
  void _showHintDialog(ValueChanged<String> onTap) {
    showModalBottomSheet<void>(
      context: Get.context!,

      /// 禁止拖动关闭
      enableDrag: true,

      /// 使用true则高度不受16分之9的最高限制
      isScrollControlled: true,
      backgroundColor: Colors.transparent,
      barrierColor: Color(0x80000000),
      builder: (_) => Container(
        color: Colors.transparent,
        height: 190,
        width: 120,
        padding: EdgeInsets.symmetric(horizontal: 16),
        child: SelectedImageUtils(
          determineBack: (String url) {
            onTap(url);
          },
        ),
      ),
    );
  }
}
